package com.infosys.cabplanningdao.emp.dao.impl;

import java.sql.Date;
import java.util.ArrayList;
import java.util.List;

import org.springframework.jdbc.core.support.JdbcDaoSupport;

import com.infosys.cabplanning.common.domain.Employee;
import com.infosys.cabplanning.common.domain.Schedule;
import com.infosys.cabplanningdao.emp.dao.EmployeeDAO;

public class EmployeeDAOImpl extends JdbcDaoSupport implements EmployeeDAO{

	@Override
	public List<Employee> getEmployeesForSchedule(Schedule schedule) {
		
		System.out.println("EmployeeDAOImpl.getEmployeesForSchedule");
		
		String sql = "SELECT emp_id, addr.address_id, addr.house_no, addr.street,"
				+ "addr.city, addr.country, addr.zip_code, "
				+ "sch.schedule_id, sch.schedule_start_time, sch.schedule_date "
				+ "FROM hackathon.employee emp, hackathon.address addr, hackathon.schedule sch WHERE "
				+ "emp.schedule_id = sch.schedule_id AND emp.address_id = addr.address_id ";
		List<Object> parameterList = new ArrayList<Object>();
		if (schedule != null && schedule.getScheduleId() != 0) {
			sql = sql +  " AND sch.schedule_id = ?";
			parameterList.add(schedule.getScheduleId());
		}
		
		if (schedule.getDate() != null) {
			sql = sql + " AND sch.schedule_date = ?";
			Date date = new Date(schedule.getDate().getTimeInMillis());
			parameterList.add(date);
		}
		
		if (schedule.getStartTime() != null && !schedule.getStartTime().isEmpty()) {
			sql = sql + " AND sch.schedule_start_time = ?";
			parameterList.add(schedule.getStartTime());
		}
		
		if (!parameterList.isEmpty()) {
			System.out.println("Full Query : "+sql);
			System.out.println(parameterList);
			List<Employee> employeeList = getJdbcTemplate().query(sql,
					parameterList.toArray(), new EmployeeRowMapper());
			System.out.println("Fetch Emp List size : "+employeeList.size());
			return employeeList;
		} else {
			//TODO Throw
			return new ArrayList<Employee>();
		}
	}
	
}
